#include "mainproject.h"
#include "ui_mainproject.h"

#include <QPushButton>
#include  "login.h"
MainProject::MainProject(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainProject)
    ,maxWindowflag(false)
{
    ui->setupUi(this);
    Logger().init();
    this->setWindowFlags(Qt::FramelessWindowHint); // 去除边框
    this->setAttribute(Qt::WA_TranslucentBackground); // 设置背景透明
    //this->setFixedSize(1280, 720); // 设置宽度为900像素，高度为500像素
    this->setWindowFlags(Qt::FramelessWindowHint); //禁用系统标题按钮
    m_pSystemManager =new SystemManager(this);
    qDebug()<<"DEBUG: main 25";
    m_pUserManager = new AdminManager(this);
    qDebug()<<"DEBUG: main 23";
    m_pDeviceManager =new DeviceManager(this);
    qDebug()<<"DEBUG: main 17";
    m_pDatamonitor = new MainWindow(this);
    qDebug()<<"DEBUG: main 21";
    m_pAlertManager = new MainWidget(this);
    qDebug()<<"DEBUG: main 19";
    // 添加主界面垂直布局
     Login* log = Login::getInstance();

    QObject::connect(log, &Login::sendLogininformation, m_pUserManager, &AdminManager::ReceiveLogininformation);
    QObject::connect(log, &Login::sendLogininformation, m_pDeviceManager, &DeviceManager::ReceiveLogininformation);
    QObject::connect(log, &Login::sendLogininformation, m_pAlertManager->get_m_pAlertManager(), &AlertManager::ReceiveLogininformation);
    QWidget *centralWidget = new QWidget(this);
    centralWidget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); //去掉拉伸
    centralWidget->setObjectName("centralWidget");
    setCentralWidget(centralWidget);

    mainVLayout=new QVBoxLayout(centralWidget);  //添加主界面垂直布局
    mainVLayout->setContentsMargins(0, 0, 0, 0);  // 设置边距为0
    mainVLayout->setSpacing(0);  // 设置布局间隔为0
    mainVLayout->addWidget(ui->titleBar);  // 添加标题栏
    qDebug()<<"DEBUG: main 28";
    qDebug()<<"DEBUG: main 25";
    mainVLayout->addWidget(m_pDeviceManager);
    qDebug()<<"DEBUG: main 27";
    mainVLayout->addWidget(m_pAlertManager);
    qDebug()<<"DEBUG: main 29";
    mainVLayout->addWidget(m_pDatamonitor);
    qDebug()<<"DEBUG: main 31";
    mainVLayout->addWidget(m_pUserManager);
    qDebug()<<"DEBUG: main 33";
    mainVLayout->addWidget(m_pSystemManager);
    qDebug()<<"DEBUG: main 35";
    onSetDatasManager();
    connects();
    dataTest= new Datatest(); //启用数据测试类
    qDebug()<<"DEBUG: main 32";


}

void MainProject::connects(){
    connect(ui->datasManagerBtn,&QPushButton::clicked,this,&MainProject::onSetDatasManager);
    connect(ui->usersManagerBtn,&QPushButton::clicked,this,&MainProject::onSetUsersManager);
    connect(ui->devicesManagerBtn,&QPushButton::clicked,this,&MainProject::onSetDevicesManager);
    connect(ui->systemManagerBtn,&QPushButton::clicked,this,&MainProject::onSetSystemManager);
    connect(ui->alertsManagerBtn,&QPushButton::clicked,this,&MainProject::onSetAlertsManager);
    connect(ui->closeBtn,&QPushButton::clicked,this,&MainProject::close);
    connect(ui->minBtn,&QPushButton::clicked,this,&MainProject::showMinimized);
    connect(ui->maxBtn,&QPushButton::clicked,this,[=](){if(!maxWindowflag){
                                                            MainProject::showMaximized();
                                                        }else{
                                                            MainProject::showNormal();
                                                        }
                                                        maxWindowflag=!maxWindowflag;});
}

void MainProject::onSetDevicesManager(){
    m_pDeviceManager->show();
    m_pAlertManager->hide();
    m_pDatamonitor->hide();
    m_pUserManager->hide();
    m_pSystemManager->hide();
}

void MainProject::onSetDatasManager(){
    m_pDeviceManager->hide();
    m_pAlertManager->hide();
    m_pDatamonitor->show();
    m_pUserManager->hide();
    m_pSystemManager->hide();
}

void MainProject::onSetAlertsManager(){
    m_pDeviceManager->hide();
    m_pAlertManager->show();
    m_pDatamonitor->hide();
    m_pUserManager->hide();
    m_pSystemManager->hide();
}

void MainProject::onSetUsersManager(){
    m_pDeviceManager->hide();
    m_pAlertManager->hide();
    m_pDatamonitor->hide();
    m_pUserManager->show();
    m_pSystemManager->hide();
}

void MainProject::onSetSystemManager(){
    m_pDeviceManager->hide();
    m_pAlertManager->hide();
    m_pDatamonitor->hide();
    m_pUserManager->hide();
    m_pSystemManager->show();
}

void MainProject::mousePressEvent(QMouseEvent *event){
    m_pMousePressedFlag=true;
    QPoint m_pMainWindow=this->geometry().topLeft();
    QPoint m_pMouse= event->globalPosition().toPoint();
    m_pMove=m_pMouse-m_pMainWindow;
}

void MainProject::mouseReleaseEvent(QMouseEvent *event){
    m_pMousePressedFlag=false;

}

void MainProject::mouseMoveEvent(QMouseEvent *event){
    if(m_pMousePressedFlag){
        QPoint m_pMouse= event->globalPosition().toPoint();
        QPoint m_pMoved=m_pMouse-m_pMove;
        this->move(m_pMoved);
    }
}

MainProject::~MainProject()
{
    delete ui;
    delete m_pDeviceManager;
    delete m_pAlertManager;
    delete m_pDatamonitor;
    delete m_pUserManager;
    delete m_pSystemManager;
    delete mainVLayout;
    delete dataTest;
}
